var app = getApp()

Page({
  data: {
    latitude: '',
    longitude: '',
    tempFilePaths: "",
    uid:"",
    markers: [],
    info: [],
    details: {},
    isShowDetails: false,
    controls: [],
    isAuth: true
  },
  onLoad: function (options) {

    const that = this

    // 查看是否授权
    wx.getSetting({

      success: function (res) {

        if (res.authSetting['scope.userInfo']) {

          //console.log('已经授权' + wx.getStorageSync('uid'));
          wx.checkSession({
            success: function () {
              
              console.log("未过期，并且在本生命周期一直有效");

              var uid = wx.getStorageSync('uid');

              if (uid) {
                app.getUserInfoFromServer(uid, function (userinfo) {

                  console.log("tt"+app.globalData.openid);

                });
              } else {
                //没有授权 默认授权
                that.setData({
                  isAuth: false
                });
              }

            },
            fail: function () {
              // session_key 已经失效，需要重新执行登录流程
              console.log("已经失效，需要重新执行登录流程");

              var uid = wx.getStorageSync('uid');

              if (uid) {

                app.getUserInfoLogin(uid, function (userinfo) {

                  console.log(app.globalData.openid);

                });
              } else {
                //没有授权 默认授权
                that.setData({
                  isAuth: false
                });
              }
            }
          })

        } else {
          that.setData({
            isAuth: false
          });
          //没有授权 默认授权
        }
      }
    })

    that.showMap()
    that.getCenter()
  },

  onReady: function () {
    this.mapCtx = wx.createMapContext('myMap')
  },
  /**
   * session_key未过期，直接从服务器获取用户信息
   */
  getUserInfoFromServer: function (uid, cb) {
   
    wx.request({
      url: app.globalData.website + 'webchat/info',
      data: {
        userId: uid
      },
      success: function (res) {
       
        if (res.data.message == "成功") {

          var userinfo = res.data.data;

          typeof cb == "function" && cb(userinfo);
        }
      }
    })
  },

  /**
   * 提交
   */
  formSubmit: function (e) {

    var that = this;

    if (e.detail.value.phone === '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'success',
        duration: 2500
      })
      return;
    }
    if (e.detail.value.reward === '') {
      wx.showToast({
        title: '赏金不能为空',
        icon: 'success',
        duration: 2500
      })
      return;
    }
    if (e.detail.value.content===''){
      wx.showToast({
        title: '描述不能为空',
        icon: 'success',
        duration: 2500
      })
      return;
    }
  
    this.getUserInfoFromServer()
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
   
    wx.request({
      
      url: app.globalData.website + 'Orderform/instOrder',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        content: e.detail.value.content,
        phone:e.detail.value.phone,
        imgpaths: this.data.tempFilePaths,
        latitude: this.data.latitude,
        longitude: this.data.longitude,
        openid: app.globalData.openid,
        reward: e.detail.value.reward
      },
      success: function (res) {

        wx.showToast({
            title: '提交成功',
          })
        that.setData({
            phone: '',
          formdata:'',
          reward:''
          })

      }
    })
  },
  /**
      * 发布的图片
      */
  showPersonal: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        that.data.tempFilePaths = res.tempFilePaths;
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      }
    })
  },
  /**
   * 获取中心点坐标
   */
  getCenter: function () {
    const that = this
    wx.getSystemInfo({
      success: function (res) {
        var width = res.windowWidth
        var height = res.windowHeight
        var left = width / 2
        var top = height / 2 - 50
        var controls = []

        that.setData({
          controls: controls
        })
      },
    })
  },
  /**
   * 绘制地图
   */
  showMap: function () {
    const that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        const lat = res.latitude
        const long = res.longitude
        that.setData({
          latitude: lat,
          longitude: long
        })

      }
    })
  },

  /**
   * 点击地图时触发事件
   */
  mapEvent: function () {
    const that = this
    that.setData({
      isShowDetails: false
    })
  },

  /**
   * 点击按钮重新定位
   */
  reLocation: function () {
    const that = this
    that.showMap()
  },
  /**
   * 获取中心位置坐标
   */
  getCenterLocation: function () {
    const that = this

    that.mapCtx.getCenterLocation({
      success: function (res) {
        var longitude = res.longitude
        var latitude = res.latitude
        console.log(app.globalData.openid);

      }
    })
  },

  /**
   * 视野发生变化时触发
   */
  regionChange: function () {
    console.log('视野变化')
    const that = this
    that.getCenterLocation()
  }
})